/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.common.MemberType;
import com.jeesite.modules.storage.dao.AddressDao;
import com.jeesite.modules.storage.dao.MemberDao;
import com.jeesite.modules.storage.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 收货地址Service
 */
@Service
@Transactional(readOnly=true)
public class AddressService extends CrudService<AddressDao, Address> {

	@Autowired
	private MemberDao memberDao;

	/**
	 * 获取会员收货地址列表
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param size 每页数量
	 * @param page 页码
	 * @return 会员收货地址列表
	 */
	@Transactional(readOnly=false)
	public List<Map> getListByMemberId(String transMemberId, String memberId, Integer size, Integer page) {
		return dao.findListByMemberId(transMemberId, memberId,size * (page - 1) , size);
	}

	/**
	 * 获取收货地址
	 * @param addressId 收货地址Id
	 * @return 收货地址
	 */
	public Map getById(String addressId) {
		return dao.findById(addressId);
	}

	/*
	* 设置其它所有收货地址
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	* */
	public void setOthersDefault(String transMemberId, String memberId){
		Address filter = new Address();
		filter.setTransMemberId(transMemberId);
		filter.setMemberId(memberId);
		List<Address> addressList = super.findList(filter);
		for (Address address : addressList){
			address.setIsDefault(false);
			address.setDefaultTime(null);
			super.save(address);
		}
	}

	/*
	 * 是否符合修改默认收货地址
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param addressId 收货地址Id
	 * @return 是否符合修改默认收货地址
	* */
	public Boolean fitModifyIsDefault(String transMemberId,String memberId, String addressId){
		if(StringUtils.isBlank(transMemberId) || !memberDao.findTypeById(memberId).equals(MemberType.VIP_TYPE)){
			return true;
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Address filter = new Address();
		filter.setTransMemberId(transMemberId);
		filter.setMemberId(memberId);
		List<Address> addressList = super.findList(filter);
		if(addressList.size() == 0){
			return true;
		}
		for (Address address : addressList){
			if(!address.getIsDefault()){
				continue;
			}
			if(simpleDateFormat.format(address.getDefaultTime()).equals(simpleDateFormat.format(new Date())) && !address.getId().equals(addressId)){
				return false;
			}
		}
		return true;
	}

	/**
	 * 保存数据
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param addressId 收货地址Id
	 * @param name 姓名
	 * @param phone 手机号
	 * @param province 省份
	 * @param city 城市
	 * @param county 县镇
	 * @param address 详细地址
	 * @param isDefault 是否默认
	 * @return 编码
	 * 200 成功
	 * 201 不符合修改【每月仅修改一次】
	 */
	@Transactional(readOnly=false)
	public Integer save(String transMemberId,String memberId, String addressId, String name, String phone, String province, String city, String county,  String address, Boolean isDefault) {
		if(isDefault ){
			if(!fitModifyIsDefault(transMemberId, memberId,addressId)) {
				return 201;
			}
			setOthersDefault(transMemberId,memberId);
		}
		Address entity = new Address();
		entity.setId(addressId);
		entity.setTransMemberId(transMemberId);
		entity.setMemberId(memberId);
		entity.setName(name);
		entity.setPhone(phone);
		entity.setProvince(province);
		entity.setCity(city);
		entity.setCounty(county);
		entity.setAddress(address);
		entity.setIsDefault(isDefault);
		if(isDefault){
			entity.setDefaultTime(new Date());
		}
		entity.setIsDeleted(false);
		super.save(entity);
		return 200;
	}

	/**
	 * 设置默认
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param addressId 收货地址Id
	 * @return 编码
	 * 200 成功
	 * 201 不符合修改【每月仅修改一次】
	 */
	@Transactional(readOnly=false)
	public Integer setIsDefault(String transMemberId,String memberId, String addressId) {
		if(!fitModifyIsDefault(transMemberId, memberId,addressId)) {
			return 201;
		}
		setOthersDefault(transMemberId,memberId);
		Address address = super.get(addressId);
		address.setIsDefault(true);
		super.save(address);
		return 200;
	}

	/**
	 * 删除数据
	 * @param addressId 收货地址Id
	 * @return
	 */
	@Transactional(readOnly=false)
	public Integer remove(String addressId) {
		dao.phyDelete(new Address(addressId));
		return 200;
	}

	/**
	 * 设置备份
	 * @param addressId 收货地址Id
	 * @return
	 */
	@Transactional(readOnly=false)
	public Address setBak( String addressId) {
		Address address = super.get(addressId);
		Address entity = new Address();
		entity.setTransMemberId(address.getTransMemberId());
		entity.setMemberId(address.getMemberId());
		entity.setName(address.getName());
		entity.setPhone(address.getPhone());
		entity.setProvince(address.getProvince());
		entity.setCity(address.getCity());
		entity.setCounty(address.getCounty());
		entity.setAddress(address.getAddress());
		entity.setIsDefault(false);
		entity.setIsDeleted(true);
		super.save(entity);
		return entity;
	}
}
